import {_decorator, Component, macro, math, Prefab, resources} from 'cc';
import {OnePolicy} from "db://assets/Scripts/MVC/Monster/OnePolicy";
import {FivePolicy} from "db://assets/Scripts/MVC/Monster/FivePolicy";
import {SixPolicy} from "db://assets/Scripts/MVC/Monster/SixPolicy";
import {MonsterCombinationType} from "db://assets/Scripts/MVC/Monster/MonsterCombinationType";
import {MonsterType} from "db://assets/Scripts/MVC/Monster/MonsterType";
import {ModelPath} from "db://assets/Scripts/Common/ModelPath";
import {EventManager} from "db://assets/Scripts/Common/EventManager";
import {EventType} from "db://assets/Scripts/Common/EventType";
import {PoolManager} from "db://assets/Scripts/framework/PoolManager";

const {ccclass, property} = _decorator;

/**
 * 敌人管理器
 */
@ccclass('MonsterManager')
export class MonsterManager extends Component {
    /**
     * 当前飞机
     */
    public monsterPrefab: Prefab = null;

    /**
     * 敌人普通飞机
     */
    private monsterNormalPrefab: Prefab = null;
    /**
     * 敌人超级战机
     */
    private monsterSuperPrefab: Prefab = null;
    /**
     * 飞机类型
     */
    private monsterTypes: MonsterType[] = [MonsterType.NORMAL, MonsterType.SUPER];

    /**
     * 飞机组合数
     */
    private monsterNumbers: MonsterCombinationType[] = [MonsterCombinationType.ONE];
    /**
     * 飞机组合类型
     */
    private combinationType: MonsterCombinationType = MonsterCombinationType.ONE;

    /**
     * 不同的组合使用不同的策略
     */
    private onePolicy: OnePolicy = new OnePolicy();
    private fivePolicy: FivePolicy = new FivePolicy();
    private sixPolicy: SixPolicy = new SixPolicy();

    /**
     * 记录用于产生不同组合敌人的游戏时间
     */
    private countTime: number = 0;

    onLoad() {
        // 初始化敌人飞机模型资源
        this.initModel();
    }

    onEnable() {
        // 注册重置事件
        EventManager.on(EventType.RESTART, this.onRestart, this);
    }

    onDisable() {
        // 取消注册重置事件
        EventManager.off(EventType.RESTART, this.onRestart, this);
    }

    start() {

    }

    update(deltaTime: number) {
        // 累计游戏时间
        this.countTime += deltaTime;
    }

    /**
     * 初始化敌人飞机模型资源
     */
    private initModel() {
        resources.load(ModelPath.monsterNormal, Prefab, (err, data) => {
            if (err) {
                console.error("敌人飞机模型资源加载失败！")
                return;
            }

            this.monsterNormalPrefab = data;
        });

        resources.load(ModelPath.monsterSuper, Prefab, (err, data) => {
            if (err) {
                console.error("敌人超级战机模型资源加载失败！");
                return;
            }

            this.monsterSuperPrefab = data;
        });
    }

    /**
     * 根据不同时间填充不同敌人数量
     */
    private fillMonsterNumbers() {
        // 0-30：组合1
        // 30-60：组合1、5、
        // 60以上：组合1、5、6
        // 调度器
        this.schedule(() => {
            this.monsterNumbers.push(this.monsterNumbers[this.monsterNumbers.length - 1] + 1);
        }, 30, 1);
    }

    /**
     * 填充敌人
     */
    private scheduleCreateMonsters() {
        this.schedule(() => {
            // 随机敌机组合类型
            this.randomEnemyCombinationType();
            // 随机敌机飞机模型
            this.randomEnemyPrefab();
            // 创建敌人飞机
            this.createEnemy();
        }, 5, macro.REPEAT_FOREVER);
    }

    /**
     * 随机敌人组合类型
     */
    private randomEnemyCombinationType() {
        const index = Math.floor(Math.random() * this.monsterNumbers.length);
        this.combinationType = this.monsterNumbers[index];
    }

    /**
     * 随机敌人飞机模型
     */
    private randomEnemyPrefab() {
        const typeIndex = math.randomRangeInt(0, 2);
        const monsterType: MonsterType = this.monsterTypes[typeIndex];
        switch (monsterType) {
            case MonsterType.NORMAL:
                this.monsterPrefab = this.monsterNormalPrefab;
                break;
            case MonsterType.SUPER:
                this.monsterPrefab = this.monsterSuperPrefab;
                break;
        }
    }

    /**
     * 创建敌人飞机
     */
    private createEnemy() {
        // 生成敌人
        switch (this.combinationType) {
            // 单个敌人
            case MonsterCombinationType.ONE:
                this.onePolicy.combination(this.monsterPrefab, this.node);
                break;
            // 一字型
            case MonsterCombinationType.FIVE:
                this.fivePolicy.combination(this.monsterPrefab, this.node);
                break;
            // V字型
            case MonsterCombinationType.SIX:
                this.sixPolicy.combination(this.monsterPrefab, this.node);
                break;
        }
    }

    /**
     * 重置事件
     */
    private onRestart() {
        // 销毁所有子节点
        this.node.destroyAllChildren();

        // 取消所有定时任务
        this.unscheduleAllCallbacks();

        // 重置计时器
        this.countTime = 0;
        // 重置飞机组合类型
        this.monsterNumbers = [MonsterCombinationType.ONE];

        // 根据不同时间填充不同敌人数量
        this.fillMonsterNumbers();
        // 创建敌人并设置位置
        this.scheduleCreateMonsters();
    }
}